package mya.mes.mdc.logic.alarm;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import mya.common.jaxrs.result.ListResult;
import mya.common.jpa.context.ServiceContext;
import mya.common.model.json.JsonUtils;
import mya.mes.mdc.local.alarm.AlarmLocal;
import mya.mes.mdc.model.alarm.MdcAlarm;
import mya.mes.mdc.model.alarm.MdcAlarmType;
import mya.mes.mdc.share.api.MdcException;

import java.util.ArrayList;
import java.util.List;

public class AlarmLocalImpl implements AlarmLocal {

    @Override
    public List<ObjectNode> queryAlarmChart(ServiceContext context, Integer chartIndex, String startDate, String endDate) throws MdcException {

        List<ObjectNode> nodeList = new ArrayList<>();

        switch (chartIndex) {

            case 1:
                nodeList = queryAlarmRate(context, startDate, endDate);
                break;
            case 2:
                nodeList = queryAlarmDayCount(context, startDate, endDate);
                break;
            case 3:
                nodeList = queryOther(context, startDate, endDate);
                break;
        }
        return nodeList;
    }

    //查询报警项百分比
    private List<ObjectNode> queryAlarmRate(ServiceContext context, String startDate, String endDate) throws MdcException {

        List list = context.em.createNamedQuery("MdcAlarm.queryRate", MdcAlarm.class)
                .setParameter(1, startDate)
                .setParameter(2, endDate)
                .getResultList();

        List<ObjectNode> nodeList = new ArrayList<>();

        List<Long> list1 = context.em.createNamedQuery("MdcAlarm.queryAllCount", Long.class)
                .setParameter(1,startDate)
                .setParameter(2,endDate)
                .getResultList();

        Long allAmount = 0l;

        if (list != null && list1.size() > 0) {
            allAmount = list1.get(0);
        } else {
            return nodeList;
        }

        for (Object o : list) {

            ObjectNode objectNode = JsonUtils.createObjectNode();
            Object[] a = (Object[]) o;
            Long rateAmount = (Long) a[0];
            Long rate = rateAmount*100/allAmount;
            Long alarmTypeId = (Long) a[1];

            MdcAlarmType alarmType = context.em.find(MdcAlarmType.class, alarmTypeId);

            String text = alarmType.getZhCN();

            objectNode.put("name", text);
            objectNode.put("value", rate);

            nodeList.add(objectNode);
        }

        return nodeList;
    }

    //查询日报警数
    private List<ObjectNode> queryAlarmDayCount(ServiceContext context, String startDate, String endDate) throws MdcException {
        List<ObjectNode> nodeList = new ArrayList<>();

        List list = context.em.createNamedQuery("MdcAlarm.queryDateCount",MdcAlarm.class)
                .setParameter(1,startDate)
                .setParameter(2,endDate)
                .getResultList();

        for(Object o:list){

            ObjectNode objectNode = JsonUtils.createObjectNode();
            Object[] a = (Object[])o;

            Long count = (Long)a[0];
            String date = (String)a[1];
            objectNode.put("count",count);
            objectNode.put("name",date);

            nodeList.add(objectNode);
        }

        return nodeList;
    }

    private List<ObjectNode> queryOther(ServiceContext context, String startDate, String endDate) throws MdcException {
        List<ObjectNode> nodeList = new ArrayList<>();
        return nodeList;
    }
}
